package com.huichenghe.xinlvsh01.slide;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import com.huichenghe.xinlvsh01.R;
import com.huichenghe.xinlvsh01.Utils.LocalDataSaveTool;
import com.huichenghe.xinlvsh01.Utils.MyConfingInfo;

public class HelpActivity extends AppCompatActivity
{
    public static final String TAG = HelpActivity.class.getSimpleName();
    private WebView mWebViewGetHelpPager;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help);
        settingTheStatebarAndNavigationbar();
        initToolbar();      // 初始化toolbar
        mWebViewGetHelpPager = (WebView)findViewById(R.id.web_view_help);
        mWebViewGetHelpPager.setWebChromeClient(new WebChromeClient());
        mWebViewGetHelpPager.setWebViewClient(new WebViewClientForHelpPager());
        mWebViewGetHelpPager.getSettings().setJavaScriptEnabled(true);
        mWebViewGetHelpPager.loadUrl(MyConfingInfo.WebRoot);
//        mWebViewGetHelpPager.loadUrl("http://www.baidu.com/");


    }

    private void initToolbar()
    {
        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar_help_activity);
        toolbar.setTitle(getString(R.string.help));
        toolbar.setTitleTextColor(getResources().getColor(R.color.White_forme));
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationIcon(R.drawable.back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if(mWebViewGetHelpPager.canGoBack())
                {
                    String urlCurrent = mWebViewGetHelpPager.getUrl();
                    if(urlCurrent.equals(MyConfingInfo.webHelpCn) || urlCurrent.equals(MyConfingInfo.webHelpEn))
                    {
                        HelpActivity.this.onBackPressed();
                    }
                    mWebViewGetHelpPager.goBack();
                }

            }
        });

    }

    /**
     * 设置导航栏和底部虚拟件全屏
     */
    private void settingTheStatebarAndNavigationbar() {
        // 透明状态栏
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //底部虚拟键透明
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {   // 控制webView前进后退
        if(keyCode == KeyEvent.KEYCODE_BACK)
        {
            if(mWebViewGetHelpPager.canGoBack())
            {
                String urlCurrent = mWebViewGetHelpPager.getUrl();
                Log.i(TAG, "当前连接地址：" + urlCurrent);
                if(urlCurrent.equals(MyConfingInfo.webHelpCn) || urlCurrent.equals(MyConfingInfo.webHelpEn)
                        || urlCurrent.equals(MyConfingInfo.webHelpCnNotConnect) || urlCurrent.equals(MyConfingInfo.webHelpEnNotConnect))
                {
                    HelpActivity.this.onBackPressed();
                }

                mWebViewGetHelpPager.goBack();
                return true;
            }
        }

        return super.onKeyDown(keyCode, event);
    }
}
